package com.me.study.designPatterns.Singleton;

import java.util.concurrent.CyclicBarrier;

/**
 * 饿汉单例
 *
 * @author ME
 * @date   2019/6/24
 */
public class HungrySingleton {

    public static void main(String[] args) {
        int count = 1000;
        CyclicBarrier barrier = new CyclicBarrier(count);
        for (int i = 0; i < count; i++) {
            new Thread(() -> {
                try {
                    barrier.await();
                    HungrySingleton.getInstance();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }, Integer.toString(i)).start();
        }
    }

    private static HungrySingleton instance = new HungrySingleton();

    private HungrySingleton() {
        System.out.println("实例化了。。。");
    }

    public static HungrySingleton getInstance() {
        return instance;
    }
}
